<template>
  <div class="department-container">
    <div class="app-container">
      <el-card shadow="always" :body-style="{ padding: '20px' }">
        <!-- 具名插槽 -->
        <template #header>
          <!-- 用一个行列布局 -->
          <el-row>
            <el-col :span="20">
              <span>{{ company.name }}</span>
            </el-col>
            <el-col :span="4">
              <el-row type="flex" justify="end">
                <!-- 两个内容 -->
                <el-col>负责人</el-col>
                <!-- 下拉菜单 element -->
                <el-col>
                  <el-dropdown>
                    <span>操作<i class="el-icon-arrow-down" /> </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item @click.native="addDept()">
                        添加子部门
                      </el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </template>
        <!-- 默认插槽 => 传入面板的内容 -->
        <el-tree
          :data="departData"
          :props="defaultProps"
          default-expand-all
          @node-click="handleNodeClick"
        >
          <template #default="{ data }">
            <!-- 单个树的项结构 -->
            <el-row style="width: 100%">
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span>操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addDept(data, 1)">
                          添加子部门
                        </el-dropdown-item>
                        <el-dropdown-item @click.native="addDept(data, 2)">
                          编辑部门
                        </el-dropdown-item>
                        <el-dropdown-item @click.native="delDept(data)">
                          删除部门
                        </el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <!-- 新增部门组件 -->
    <AddDept
      ref="addEdit"
      :all-dept="allDept"
      :show="show"
      :curr-dept="currDept"
      @updata-list="getDepartData"
      @close-dialog="show = $event"
    ></AddDept>
  </div>
</template>

<script>
import { getDepartments, delDepartments } from '@/api/departments'
import { transformTreeData } from '@/utils'
// 新增部门
import AddDept from './components/add-dept.vue'
export default {
  components: {
    AddDept
  },
  data () {
    return {
      // 所有部门数据
      allDept: [],
      // 当前要添加的部门(父)
      currDept: {},
      // 弹层显示隐藏
      show: false,
      // 组织架构树形数据
      departData: [
        {
          name: '总裁办'
        },
        {
          name: '财务部',
          children: [
            {
              name: '财务核算部'
            },
            {
              name: '财务税务部'
            }
          ]
        }
      ],
      // 公司信息
      company: { name: '', manager: 'CEO' },
      defaultProps: {
        children: 'children',
        label: 'name' // 后台返回的叫name
      }
    }
  },
  created () {
    this.getDepartData()
  },
  methods: {
    // 新增部门
    /**
     * currDept 当前要添加的部门
     */
    addDept (currDept, type) {
      console.log(type === 1 ? '新增' : '编辑')
      this.currDept = currDept
      if (type === 2) {
        this.$refs.addEdit.getDeptDetail(currDept.id)
      }
      this.show = true
    },
    // 删除部门
    async delDept (currData) {
      console.log('当前要删除的部门信息: ', currData)
      /**
       * 1. 用户确认
       * 2. 确定了 => 判断如果是父部门且下面有子部门 (不能删除)
       * 3. 如果没有子部门, 可以删除
       * 4. 从新渲染数据
       */
      try {
        await this.$confirm(`确认要删除${currData.name}吗`, '提示')
        // 确定走到这
        if (currData.children && currData.children.length > 0) {
          return this.$message.error('不能删除有子的父部门, 请先删除子')
        }
        await delDepartments(currData.id)
        this.getDepartData()
        this.$message.success('删除成功')
      } catch (error) {
        // 取消走到这
        console.log(error)
      }
    },
    // 获取部门数据
    async getDepartData () {
      const { companyName, depts } = await getDepartments()
      console.table(depts)
      this.allDept = depts
      this.company.name = companyName
      // console.log('转换结果: ', transformTreeData(depts))
      this.departData = transformTreeData(depts)
    },
    handleNodeClick (data) {
      console.log(data)
    }
  }
}
</script>

<style lang="scss" scoped>
// 深度作用选择符 less 中使用/deep/ | scss 中使用 ::v-deep
::v-deep .el-tree-node__content {
  margin: 3px 0;
  font-size: 14px;
}
</style>
